//
//  PersonInfoViewVC.swift
//  movieInfo
//
//  Created by zhangkai on 2023/5/14.
//

import UIKit

class PersonInfoViewVC: BaseVC {
    
//    var personinfo: Person!
    var id:Int = 0
    
    var info:PersonInfoView!
    var movies:ViewControllerMoviesInfoView!
    


    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        // Do any additional setup after loading the view.
        let w = UIScreen.main.bounds.width
        
        info = PersonInfoView()
        view.addSubview(info)
        info.snp.makeConstraints { make in
            make.top.equalTo(topLayoutGuide.snp.bottom)
            make.left.equalToSuperview()
            make.width.equalTo(w)
            make.height.equalTo(400)
        }
        info.backgroundColor = .white
        info.img.contentMode = .scaleAspectFill
        info.img.clipsToBounds = true
        
        
        movies = ViewControllerMoviesInfoView()
        movies.callBlock = { [weak self] model in
            
            
            let vc = ViewControllerInfoVC()
            vc.model = model
            vc.hidesBottomBarWhenPushed = true
            self?.navigationController?.pushViewController(vc, animated: true)
       
        }
       
        view.addSubview(movies)
        movies.snp.makeConstraints { make in
            make.top.equalTo(info.snp.bottom).offset(10)
            make.left.equalToSuperview()
            make.width.equalTo(w)
            make.height.equalTo(170)
        }
        movies.backgroundColor = .white
        movies.infoLabel.text = "     Known for:"
        movies.infoLabel.backgroundColor = .secondarySystemBackground
//        infoLabel
        
        getInfo()
        
    }
    
    
    func getInfo(){
        
        
        let url = MMNetWork.shared.getPersonDetailsURL(personID: self.id)
        MMNetWork.shared.fetchData(urlString: url, castType: Person.self) { result in
            switch result {
            case .success(let person):
//                completion(.success(person))
                
                
                DispatchQueue.main.async {
                    self.configUI(model: person)
                }
                
                
                print(person)
            case .failure(let error):
//                completion(.failure(error))
                print(error)
            }
        }

        
        
        
        
        

    
        let  urltype = "person/\(self.id)/movie_credits?"
        let  keypath = "cast"
     
        let requestURL = MMNetWork.shared.searchMovieURL(type: urltype, page: 1)
        MMNetWork.shared.fetchData(urlString: requestURL, castType: [Movie].self, keyPath: keypath) { [weak self] result in
            guard let self = self else { return }
            switch result {
            case .success(let movies):
                
                DispatchQueue.main.async {
                    self.configMovies(models: movies)
                }
  
            case .failure(let error):
                print(error)
            }
        }
        

        
        
        
    }
    
    func configMovies(models:[Movie]){
        
        movies.model = models
        
    }
    
    func configUI(model:Person){
        
        
        if let path = model.profilePath {
            info.img.downloadImage(fromPath: path)
        }
        info.nameLabel.text = model.name
        info.timeLabel.text = "Born:" + (model.birthday ?? "")
//        info.infoLabel.text = model.biography
        info.infoView.text = model.biography

        
    }

}
